package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.Position;
import cn.edu.cdu.group4nb.po.Recruit;
import cn.edu.cdu.group4nb.service.RecruitService;
import cn.edu.cdu.group4nb.utils.R;
import cn.edu.cdu.group4nb.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class RecruitController {
    @Autowired
    private RecruitService recruitService;
    /**
     * 根据institution_id查询招聘用的position（包括salary）
     * @return map
     */
    @ResponseBody
    @GetMapping("/recruitmentManager/selectAllPositionByInstitutionId")
    public List<Map<String,Object>> selectAllPositionByInstitutionId (Integer institutionId){
//        int id = Integer.parseInt(institutionId);s
        List<Map<String,Object>> allPositionByInstitutionId = recruitService.selectAllPositionByInstitutionId(institutionId);
        return  allPositionByInstitutionId;
    }
    /**
     * 查询登录用户所在机构
     * @return Institution
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/selectInstitutionOfLoginUser")
    public R selectInstitutionOfLoginUser (){
        return R.ok().put("institutionOfLoginUser", recruitService.selectInstitutionOfLoginUser(ShiroUtils.getUserId()));
    }
    /**
     * 根据登录用户查询其机构下的所有岗位
     * @return Position
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/selectPositionByInstitutionIdOfLoginUser")
    public R selectPositionByInstitutionIdOfLoginUser (){
        List<Position> PositionByInstitutionIdOfLoginUser = recruitService.selectPositionByInstitutionIdOfLoginUser(ShiroUtils.getUserId());
        return  R.ok().put("PositionByInstitutionIdOfLoginUser",PositionByInstitutionIdOfLoginUser);
    }
    /**
     * 根据机构id查询机构下的单个岗位人数
     * @return int
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/getPositionNumByInstitutionIdAndPositionId")
    public R getPositionNumByInstitutionIdAndPositionId (
//            @RequestParam Integer institutionId,@RequestParam Integer positionId
            @RequestBody Map<String,Object> map
    ){
        int institutionId = Integer.parseInt(String.valueOf(map.get("institutionId")));
        int positionId = Integer.parseInt(String.valueOf(map.get("positionId")));
//        System.out.println("institutionId:"+institutionId);
//        System.out.println("positionId:"+positionId);
        int PositionNumByInstitutionIdAndPositionId = recruitService.getPositionNumByInstitutionIdAndPositionId(institutionId,positionId);
//        System.out.println("PositionNumByInstitutionIdAndPositionId:"+PositionNumByInstitutionIdAndPositionId);
        return R.ok().put("PositionNumByInstitutionIdAndPositionId", PositionNumByInstitutionIdAndPositionId);
    }
    /**
     * 新增招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/insertRecruit")
    public R insertRecruit (@RequestBody Recruit record){
        recruitService.insertRecruit(record);
        return R.ok();
    }
    /**
     * 查询招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/selectAllRecruit")
    public List<Map<String,Object>> selectAllRecruit (){
        System.out.println("AllRecruit:"+recruitService.selectAllRecruit());
        return  recruitService.selectAllRecruit();
    }
    /**
     * 查询登录用户角色
     * @return roleid
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/getRoleIdOfLoginUser")
    public R getRoleIdOfLoginUser (){
        return R.ok().put("roleIdOfLoginUser", recruitService.getRoleIdOfLoginUser(ShiroUtils.getUserId()));
    }
    /**
     * 删除招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/deleteRecruitByIds")
    public R deleteRecruitByIds (@RequestBody int[] recruitId){
        for(int g:recruitId){
            System.out.println(g);
        }
        recruitService.deleteRecruitByIds(recruitId);
        return R.ok();
    }
    /**
     * 更新招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/updateRecruit")
    public R updateRecruit (@RequestBody Recruit record){
        System.out.println("updateRecruit:"+record);
        recruitService.updateRecruit(record);
        return R.ok();
    }
    /**
     * 根据id查招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/selectRecruitById")
    public R selectRecruitById (@RequestBody Integer recruitId){
        List<Map<String,Object>> RecruitById = recruitService.selectRecruitById(recruitId);
        return  R.ok().put("RecruitById",RecruitById);
    }
    /**
     * 根据id发布招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/publishRecruit")
    public R publishRecruit (@RequestBody Recruit record){
        System.out.println("Recruit:"+record);
        Date date=new Date();
//        DateFormat df1 = DateFormat.getDateInstance();//日期格式，精确到日
//        System.out.println(df1);
//        System.out.println(df1.format(date));
        record.setReleaseDate(date);
        System.out.println("Recruit:"+record);
        recruitService.publishRecruit(record);
        return R.ok();
    }
    /**
     * 查询已发布招聘信息
     * @return
     */
    @ResponseBody
    @PostMapping("/recruitmentManager/listRecruit")
    public List<Map<String,Object>> listRecruit (){
        System.out.println("listRecruit:"+recruitService.listRecruit());
        return  recruitService.listRecruit();
    }
}

